package com.woniuxy.controller;


import cn.hutool.core.io.FileUtil;
import com.woniuxy.common.Result;
import com.woniuxy.controller.form.FoodVarietyInsertForm;
import com.woniuxy.controller.form.FoodVarietySearchForm;
import com.woniuxy.controller.form.FoodVarietyUpdateForm;
import com.woniuxy.controller.form.FoodVarietyUpdateStatusForm;
import com.woniuxy.entity.FoodVariety;
import com.woniuxy.service.FoodVarietyService;
import com.woniuxy.service.dto.FoodVarietyDto;
import com.woniuxy.service.dto.WoniuPageDto;
import com.woniuxy.util.GetTypeByHead;
import com.woniuxy.util.OSSUtil;
import com.woniuxy.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

/**
 * <p>
 * 菜品 前端控制器
 * </p>
 *
 * @author zz
 * @since 2023年06月13日
 */
@RestController
@RequestMapping("/foodVariety")
@Api(tags = "菜品相关接口")
public class FoodVarietyController {

    @Autowired
    private FoodVarietyService service;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OSSUtil ossUtil;

    @PostMapping("/insert")
    @ApiOperation("新增菜品")
    public Result insert(@RequestBody FoodVarietyInsertForm form, @RequestHeader String accessToken) throws Exception {
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        FoodVariety foodVariety=service.insert(form,accountId);
        return Result.success(200,"新增菜品成功",foodVariety);
    }

    //菜品上下架 删除 设置招牌 --update
    @PostMapping ("/updateStatus")
    @ApiOperation("上下架菜品或删除菜品")
    public Result updateStatus(@RequestBody FoodVarietyUpdateStatusForm form, @RequestHeader String accessToken) throws Exception {
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        service.updateStatus(form,accountId);
        return Result.success(200,"修改菜品状态成功",null);

    }

    @GetMapping ("/search")
    @ApiOperation("条件查看所有菜品")
    public Result search(FoodVarietySearchForm form, @RequestHeader String accessToken) throws Exception {
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        WoniuPageDto<FoodVarietyDto> dto = service.searchByCondition(form,accountId);
        return Result.success(200,"商家查看所有菜品成功",dto);
    }

    @PostMapping ("/update")
    @ApiOperation("商家修改菜品信息")
    public Result update(@RequestBody FoodVarietyUpdateForm form, @RequestHeader String accessToken) throws Exception {
        Integer accountId = redisUtil.getUserIdByToken(accessToken);
        service.updateMessage(form,accountId);
        return Result.success(200,"修改菜品基本信息成功",null);
    }

    @PostMapping ("/setSpecialty")
    @ApiOperation("设为招牌")
    public Result setSpecialty(@RequestBody FoodVarietyUpdateForm form, @RequestHeader String accessToken) throws Exception {
        service.lambdaUpdate().set(FoodVariety::getSortNum, form.getSortNum())
                .eq(FoodVariety::getId, form.getId())
                .update();
        return Result.success(200,"修改菜品基本信息成功",null);
    }

    @PostMapping("/upload")
    @ApiOperation("上传图片")
    public Result upload(MultipartFile file) throws Exception {
        String fileType = GetTypeByHead.getFileHeader(file);
        if ( !(fileType.equals("jpg")|| fileType.equals("png")|| fileType.equals("tif") ||fileType.equals("jpeg"))){
            throw new Exception("请上传图片类型的文件");
        }
        InputStream is = file.getInputStream();
        String fileOriginalFilename = file.getOriginalFilename();
        String suffix = FileUtil.getSuffix(fileOriginalFilename);
        String newFileName = UUID.randomUUID().toString().replace("_","");
        String url = ossUtil.uploadFile(is, newFileName + "." + suffix, "bucket-book-fjx");
        return Result.success(200,"上传图片成功",url);
        //https://bucket-book-fjx.oss-cn-chengdu.aliyuncs.com/606b7e7c-addf-4e65-8b2f-7e4bea2b4157.jpeg
    }

    @PostMapping("/uploadfile")
    @ApiOperation("上传附件文件")
    public Result uploadFile(MultipartFile file) throws Exception {
        InputStream is = file.getInputStream();
        String fileOriginalFilename = file.getOriginalFilename();
        String suffix = FileUtil.getSuffix(fileOriginalFilename);
        String newFileName = UUID.randomUUID().toString().replace("_","");
        String url = ossUtil.uploadFile(is, newFileName + "." + suffix, "bucket-book-fjx");
        return Result.success(200,"上传文件成功",url);
        //https://bucket-book-fjx.oss-cn-chengdu.aliyuncs.com/447559c7-e787-4627-8360-974a9122af3b.pdf
    }


}

